package com.jindy.jpush.utils;

import com.alibaba.fastjson2.JSON;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.jpush.domain.MyJpushClient;
import com.jindy.jpush.domain.MyNotification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author yqq
 * @date 2023/4/2
 * @Description
 */
@RestController
@RequestMapping("/api")
public class JPushUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(JPushUtils.class);
    @Resource
    private MyJpushClient myJPushClient;

    @Value("${jpush.appKey}")
    private String appKey;

    @Value("${jpush.masterSecret}")
    private String masterSecret;

    @Value("${jpush.apnsProduction}")
    private boolean apnsProduction;

    /**
     * 通过别名通知推送
     */
    @GetMapping("/message")
    public void myJPushClientByAlias(List<String> aliasList,String alert) {
        try {
            MyNotification myNotification = new MyNotification();
            myNotification.setTitle("湿云");
            myNotification.setAlert(alert);
            HashMap<String, String> extras = new HashMap<>();
            extras.put("业务key1","业务value1");
            extras.put("业务key2","业务value2");
            extras.put("业务key3","业务value3");
            myNotification.setExtras(extras);
            myJPushClient.sendToAliasList(aliasList,myNotification);
        } catch (Exception e) {
            LOGGER.error("JPushUtils.myJPushClientByAlias-通过别名通知推送失败: ",e);
        }
    }

    /**
     * 绑定别名与设备
     *
     */
    public AjaxResult bindAliasAndDevice(String alias, String registrationId) {
        String result="";
        try {
            result = myJPushClient.bindAliasAndDevice(alias, registrationId);
        } catch (Exception e) {
            LOGGER.error("绑定别名与设备失败",e);
            return AjaxResult.error("绑定别名与设备失败");
        }
        return AjaxResult.success(result);
    }

    /**
     * 测试解绑别名与设备
     */
    void testUnbindAliasAndDevice() {
        String result = myJPushClient.unbindAliasAndDevice("26143", "18171adc03c5f393818");
    }

    /**
     * 测试查询别名
     */
    void testGetAliasDevice() {
        String result = myJPushClient.getAliasDevice("26143");
        if (result != null) {
            String registration_ids = JSON.parseObject(result).getString("registration_ids");
            List<String> ids = JSON.parseArray(registration_ids, String.class);
        }
    }

    /**
     * 删除别名
     */
    @GetMapping("/delete/{alias}")
    public AjaxResult deleteAliasDevice(@PathVariable String alias) {
        String result="";
        try {
            result = myJPushClient.deleteAliasDevice(alias);
        } catch (Exception e) {
            LOGGER.error("删除别名",e);
            return AjaxResult.error("删除别名");
        }
        return AjaxResult.success(result);
    }
}
